﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTBusiness.DB_Linq;
using TTBusiness.ClassDB;
using TTBusiness;

namespace TTMarketing.Admin
{
    public partial class Administrator : System.Web.UI.Page
    {
        clsBSAdmin accbs = new clsBSAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            divmessage.InnerHtml = "";
            divmessage2.InnerHtml = "";
        }

        protected void cmbClose_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Convert.ToInt32(e.Value) == 1)
            {
                plDetail.Visible = true;
            }
            else
            {
                plDetail.Visible = false;
            }
        }

        protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(rgdGrid.Items[rgdGrid.SelectedIndexes[0]]["ID"].Text);
                tblAdmin acc = new tblAdmin();
                acc = accbs.getById(id);
                this.plDetail.Visible = true;
                this.txtEmail.Text = acc.Email;
                this.txtFullName.Text = acc.FullName;
                this.txtUserName.Text = acc.UserName;
                this.cmbOrderRequest.SelectedValue = acc.OrderRequest.Equals(true) ? "1" : "0";
                this.hfId.Value = id.ToString();
                this.cmbClose.SelectedValue = "1";
            }
            catch (Exception ex)
            {
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }

        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (accbs.getByUserName(this.txtUserName.Text) == null)
                {
                    tblAdmin acc = new tblAdmin();
                    acc.Email = this.txtEmail.Text;
                    acc.FullName = this.txtFullName.Text;
                    acc.OrderRequest = this.cmbOrderRequest.SelectedValue.Equals("1") ? true : false;
                    acc.UserName = this.txtUserName.Text;
                    accbs.insert(acc);
                    divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Thêm mới thành công").ToString();
                    this.rgdGrid.Rebind();
                }
                else
                {
                    divmessage.InnerHtml = clsBSMessageAdmin.MessageWarning("Cảnh báo : ", "Account đã tồn tại").ToString();
                }
            }
            catch (Exception ex)
            {
                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể thêm mới : " + ex.Message).ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                tblAdmin tmp= new tblAdmin();
                tmp = accbs.getByUserName(this.txtUserName.Text);
                if ( tmp== null||tmp.ID.ToString().Equals(hfId.Value))
                {
                    tblAdmin acc = new tblAdmin();
                    acc.Email = this.txtEmail.Text;
                    acc.FullName = this.txtFullName.Text;
                    acc.OrderRequest = this.cmbOrderRequest.SelectedValue.Equals("1") ? true : false;
                    acc.UserName = this.txtUserName.Text;
                    acc.ID = Convert.ToInt32(hfId.Value);
                    accbs.update(acc);
                    divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Cập nhật thành công").ToString();
                    this.rgdGrid.Rebind();
                }
                else
                {
                    divmessage.InnerHtml = clsBSMessageAdmin.MessageWarning("Cảnh báo : ", "Account đã tồn tại").ToString();
                }
            }
            catch (Exception ex)
            {
                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể cập nhật : " + ex.Message).ToString();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                accbs.resetPassword(Convert.ToInt32(hfId.Value));
                divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Reset mật khẩu thành công").ToString();
            }
            catch (Exception ex)
            {

                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi ko thể đổi dc mật khẩu " + ex.Message).ToString();
            }
        }

        protected void rgdGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("delete"))
                {
                    accbs.delete(Convert.ToInt32(e.CommandArgument));
                    divmessage2.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Đã xóa thành công!").ToString();
                    this.rgdGrid.Rebind();
                    if (this.plDetail.Visible && !String.IsNullOrEmpty(hfId.Value) && Convert.ToInt32(hfId.Value).Equals(Convert.ToInt32(e.CommandArgument)))
                    {
                        btnClear_Click(null, null);
                    }
                }
            }
            catch (Exception)
            {
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể xóa được").ToString();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtEmail.Text = "";
            this.txtFullName.Text = "";
            this.txtUserName.Text = "";
            this.cmbOrderRequest.Items[0].Selected = true;
            this.hfId = null;
        }
    }
}